package com.hannahxian.easseyjoke.recycleviewhead;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

/**
 * Created by Broderick
 * User: Broderick
 * Date: 2017/5/10
 * Time: 15:07
 * Version: 1.0
 * Description:
 * Email:wangchengda1990@gmail.com
 **/
public class WrapRecycleView extends RecyclerView {

	private WrapRecyclerAdapter mWrapRecyclerAdapter;

	private RecyclerView.Adapter mAdapter;

	public WrapRecycleView(Context context) {
		super(context);
	}

	public WrapRecycleView(Context context, @Nullable AttributeSet attrs) {
		super(context, attrs);
	}

	public WrapRecycleView(Context context, @Nullable AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	public void setAdapter(Adapter adapter) {

		// 为了防止多次设置Adapter
		if (mAdapter != null) {
			mAdapter.unregisterAdapterDataObserver(mDataObserver);
			mAdapter = null;
		}

		this.mAdapter = adapter;

		if (adapter instanceof WrapRecyclerAdapter) {
			mWrapRecyclerAdapter = (WrapRecyclerAdapter) adapter;
		} else {
			mWrapRecyclerAdapter = new WrapRecyclerAdapter(adapter);
		}

		super.setAdapter(mWrapRecyclerAdapter);

		// 注册一个观察者
		mAdapter.registerAdapterDataObserver(mDataObserver);

		// 解决GridLayout添加头部和底部也要占据一行
		mWrapRecyclerAdapter.adjustSpanSize(this);
	}
	// 添加头部
	public void addHeaderView(View view) {
		// 如果没有Adapter那么就不添加，也可以选择抛异常提示
		// 让他必须先设置Adapter然后才能添加，这里是仿照ListView的处理方式
		if (mWrapRecyclerAdapter != null) {
			mWrapRecyclerAdapter.addHeaderView(view);
		}
	}

	// 添加底部
	public void addFooterView(View view) {
		if (mWrapRecyclerAdapter != null) {
			mWrapRecyclerAdapter.addFooterView(view);
		}
	}

	// 移除头部
	public void removeHeaderView(View view) {
		if (mWrapRecyclerAdapter != null) {
			mWrapRecyclerAdapter.removeHeaderView(view);
		}
	}

	// 移除底部
	public void removeFooterView(View view) {
		if (mWrapRecyclerAdapter != null) {
			mWrapRecyclerAdapter.removeFooterView(view);
		}
	}

	private AdapterDataObserver mDataObserver = new AdapterDataObserver() {
		@Override
		public void onChanged() {
			if (mAdapter == null) return;
			// 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
			if (mWrapRecyclerAdapter != mAdapter)
				mWrapRecyclerAdapter.notifyDataSetChanged();
		}

		@Override
		public void onItemRangeRemoved(int positionStart, int itemCount) {
			if (mAdapter == null) return;
			// 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyDataSetChanged没效果
			if (mWrapRecyclerAdapter != mAdapter)
				mWrapRecyclerAdapter.notifyItemRemoved(positionStart);
		}

		@Override
		public void onItemRangeMoved(int fromPosition, int toPosition, int itemCount) {
			if (mAdapter == null) return;
			// 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyItemMoved没效果
			if (mWrapRecyclerAdapter != mAdapter)
				mWrapRecyclerAdapter.notifyItemMoved(fromPosition, toPosition);
		}

		@Override
		public void onItemRangeChanged(int positionStart, int itemCount) {
			if (mAdapter == null) return;
			// 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyItemChanged没效果
			if (mWrapRecyclerAdapter != mAdapter)
				mWrapRecyclerAdapter.notifyItemChanged(positionStart);
		}

		@Override
		public void onItemRangeChanged(int positionStart, int itemCount, Object payload) {
			if (mAdapter == null) return;
			// 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyItemChanged没效果
			if (mWrapRecyclerAdapter != mAdapter)
				mWrapRecyclerAdapter.notifyItemChanged(positionStart,payload);
		}

		@Override
		public void onItemRangeInserted(int positionStart, int itemCount) {
			if (mAdapter == null) return;
			// 观察者  列表Adapter更新 包裹的也需要更新不然列表的notifyItemInserted没效果
			if (mWrapRecyclerAdapter != mAdapter)
				mWrapRecyclerAdapter.notifyItemInserted(positionStart);
		}
	};
}
